﻿<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.CellEditContext.OnChanged(System.String-System.Object)">
    <CodeSnippetTabPage Text="Razor">@(@"<DxDataGrid Data=""@Vacancies""
            ShowPager=""false""
            RowRemoving=""@((dataItem) => OnRowRemoving(dataItem))""
            RowInserting=""@((newValues) => OnRowInserting(newValues))""
            RowUpdating=""@((updatingDataItem, newValues) => OnRowUpdating(updatingDataItem, newValues))""
            CssClass=""mw-1100"">
    <DxDataGridCommandColumn Width=""150px""></DxDataGridCommandColumn>
    <DxDataGridColumn Field=""@nameof(Vacancy.Description)"" Caption=""Vacancy""></DxDataGridColumn>

    <DxDataGridComboBoxColumn Field=""@nameof(Vacancy.Region)"" Data=""@VacancyRepository.Regions"">
        <EditTemplate>
            @{
                var editingContext = (CellEditContext)context;
                var region = (string)editingContext.GetEditorValue(nameof(Vacancy.Region));
            }
            <DxComboBox Data=""@VacancyRepository.Regions""
                        NullText=""Select Region...""
                        Value=""@region""
                        ValueChanged=""@(newCellValue => {
                                            editingContext.OnChanged(newCellValue);
                                            editingContext.OnChanged(nameof(Vacancy.City), null);
                                            OnRegionChanged();
                                            })"">
            </DxComboBox>
        </EditTemplate>
    </DxDataGridComboBoxColumn>
    <DxDataGridColumn Field=""@nameof(Vacancy.City)"">
        <EditTemplate>
            @{
                var editingContext = (CellEditContext)context;

                var region = (string)editingContext.GetEditorValue(nameof(Vacancy.Region));
                var city = ((string)(editingContext.GetEditorValue(nameof(Vacancy.City))));

                var cities = VacancyRepository.GetOfficeLocationsByRegion(region).Select(x => x.City);
            }
            <DxComboBox Data=""@cities""
                        NullText=""Select City...""
                        Text=""@city""
                        ValueChanged=""@(newCellValue => editingContext.OnChanged(newCellValue))"">
            </DxComboBox>
        </EditTemplate>
    </DxDataGridColumn>
</DxDataGrid>

@code {
    IEnumerable<Vacancy> Vacancies;

    protected override async Task OnInitializedAsync()
    {
        Vacancies = await VacancyRepository.GetVacancies(3);
    }

    public void OnRegionChanged()
    {
        InvokeAsync(StateHasChanged);
    }
    void OnRowUpdating(Vacancy vacancy, Dictionary<string, object> newValue)
    {
        foreach (var field in newValue.Keys)
        {
            switch (field)
            {
                case ""Description"":
                    vacancy.Description = (string)newValue[field];
                    break;
                case ""Region"":
                    vacancy.Region = (string)newValue[field];
                    break;
                case ""City"":
                    vacancy.City = (string)newValue[field];
                    break;
            }
        }
    }
    void OnRowRemoving(Vacancy vacancy)
    {
        Vacancies = Vacancies.Where(v => v != vacancy).ToArray();
        InvokeAsync(StateHasChanged);
    }
    void OnRowInserting(Dictionary<string, object> newValue)
    {
        var newVacancy = new Vacancy();
        OnRowUpdating(newVacancy, newValue);
        Vacancies = (new Vacancy[] { newVacancy }).Concat(Vacancies);

        InvokeAsync(StateHasChanged);
    }
}
")</CodeSnippetTabPage>
</CodeSnippetTabbed>
